package controllers;

import play.*;
import play.mvc.*;
import hprose.HproseClinetService;

import java.util.*;

import constants.Constants;
import models.*;

public class Application extends Controller {

    public static void index() {
    	//查询出接口模块状态
    	List<ModelCfg> models = ModelCfg.find("order by id asc").fetch();
    	int size = 0;
    	if(models!=null){
    		size = models.size();
    	}
    	List<WorkersCfg> workers = WorkersCfg.findAll();
    	int ws = 0;
    	if(workers!=null){
    		ws = workers.size();
    	}
    	String ip=Constants.HPROSE_HOST;
    	int port = Constants.HPROSE_PORT;
    	String platName=Constants.PLAT_NAME;
        render(models,size,workers,ws,ip,port,platName);

    }
    
    public static void addModels(){
    	ModelCfg models = new ModelCfg();
    	models.alias = params.get("alias");
    	models.description = params.get("description");
    	models.className = params.get("className");
    	models.modelName = params.get("modelName");
    	models.createTime = new Date();
    	models.creatorId = "admin";
    	models.state = ModelCfg.ONLINE;
    	models.save();
    	index();
    }
    public static void addWorker(){
    	WorkersCfg worker = new WorkersCfg();
    	worker.className=params.get("className");
    	worker.createTime = new Date();
    	worker.creatorId="admin";
    	worker.description=params.get("description");
    	worker.host = params.get("host");
    	worker.state = WorkersCfg.ONLINE;
    	worker.workerName=params.get("workerName");
    	worker.save();
    	index();
    }
    
    /**
     * 测试model
     */
    public static void testModel(){
    	String a = params.get("a");
    	String b = params.get("b");
    	String c = params.get("c");
    	String ret= "";
    	try{
	    	String callName = a+"_"+b;
	    	String[] ars = c.split("&");
	    	Map<String,Object> map = new HashMap<String,Object>();
	    	for(String ss : ars){
	    		map.put(ss.split("=")[0], ss.split("=")[1]);
	    	}
    	
	    	Object[] args = new Object[]{map};
	    	ret = HproseClinetService.synCall(callName, args, Constants.HPROSE_HOST, Constants.HPROSE_PORT);
    	}catch(Exception e){
    		Logger.info("请检查参数拼写");
    		renderText("请检查参数拼写");
    	}
    	
    	
    	Logger.info("处理结果:"+ret);
    	renderText(ret);
    }

}